﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.ServiceLocation;
using Subfaculty.Common.Entities;
using Subfaculty.Common.Interfaces.Services;
using Subfaculty.WebApplication.ViewModels;

namespace Subfaculty.WebApplication.Translators
{
	public class StudyGroupTranslator : BaseTranslator<StudyGroup, StudyGroupViewModel>
	{
		private IStudyYearRepository _studyYearRepository;
		private IStudyAreaRepository _studyAreaRepository;

		public StudyGroupTranslator()
		{
			_studyYearRepository = ServiceLocator.Current.GetInstance<IStudyYearRepository>();
			_studyAreaRepository = ServiceLocator.Current.GetInstance<IStudyAreaRepository>();
		}

		public override StudyGroupViewModel Translate(StudyGroup source)
		{
			var pe = new StudyGroupViewModel();

			pe.Id = source.Id;
			pe.StudyAreaId = source.StudyArea.Id;
			pe.StudyArea = source.StudyArea.Code + " " + source.StudyArea.Name + " " + source.StudyArea.Profile;

			pe.StudyYearId = source.StartStudyYear.Id;
			pe.StudyYear = source.StartStudyYear.StartDate + " - " + source.StartStudyYear.EndDate;

			
			return pe;
		}

		public override StudyGroup TranslateBack(StudyGroupViewModel pe, StudyGroup entity)
		{
			entity.Id = pe.Id;

			entity.StudyArea = _studyAreaRepository.Get(pe.StudyAreaId);
			entity.StartStudyYear = _studyYearRepository.Get(pe.StudyYearId);

			return entity;
		}
	}
}